/*
 * @Description:
 * @Author: Zzt
 * @Date: 2022-11-10 16:49:18
 * @LastEditTime: 2023-02-10 20:13:52
 */
import { createContext, useReducer, useEffect, useContext } from 'react';
import { message, Modal } from 'antd';
import {
  SET_SELECT_FOND,
  SET_SELECT_AT,
  SET_TPL_TYPE,
  SET_SELECTED_ITEM,
  SET_TEMPLATE_LIST,
  ADD_TEMPLATE,
  EDIT_TEMPLATE,
  DELETE_TEMPLATE
} from '../constants';
import { DetectionTemplateReducer } from '../reducers/DetectionTemplateReducer';
import {
  detectionTemplateByPage,
  addDetectionTemplate,
  deleteDetectionTemplate,
  editeDetectionTemplate
} from '@/api/standardizedDetection/detectionTemplate';
import { TemplateTypes } from '@/view/system/DetectionTemplate/dictionaries';

const initialState = {
  // 当前选中的项目
  selectFond: {},
  selectAT: {},
  // 检测模板类型：1、条目与原文检测 2、条目检测 3、原文检测 4、EEP格式检测 5、光盘格式检测 6、系统环境检测；
  detectionType: 1,
  tplTypeList: [],
  selectItems: [],
  templateList: [],
  typeStatusList: [
    {
      code: TemplateTypes.EntryAndOriginal,
      label: '条目与原文检测',
      key: 1
    },
    { code: TemplateTypes.Entry, label: '条目检测', key: 2 },
    { code: TemplateTypes.Original, label: '原文检测', key: 3 },
    { code: TemplateTypes.EEP, label: 'EEP格式检测', key: 4 },
    { code: TemplateTypes.CD, label: '光盘格式检测', key: 5 },
    { code: TemplateTypes.System, label: '系统环境检测', key: 6 }
  ]
};
const DetectionTemplateContext = createContext();
const DetectionTemplateContextProvider = (props) => {
  const [state, dispatch] = useReducer(DetectionTemplateReducer, initialState);
  const actions = {
    /**
     * @description: 设置选中的全宗
     */
    setSelectFond(data) {
      dispatch({ type: SET_SELECT_FOND, data });
    },

    /**
     * @description: 设置选中的档案类型
     */
    setSelectAT(data) {
      dispatch({ type: SET_SELECT_AT, data });
    },

    setTemplateType(data) {
      dispatch({ type: SET_TPL_TYPE, data });
    },
    /**
     * @description: 设置表格中选中条目的key
     * @param {*} selectArr
     * @return {*}
     */
    setSelectKey(selectArr) {
      dispatch({ type: SET_SELECTED_ITEM, data: selectArr });
    },
    /**
     * @description: 请求档案分类列表
     * @param {object} params 请求参数
     * @return {*}
     */
    getTemplateList(params) {
      return new Promise((resolve, reject) => {
        detectionTemplateByPage(params)
          .then((res) => {
            const { data } = res;
            dispatch({ type: SET_TEMPLATE_LIST, data: data.results });
            resolve(res);
          })
          .catch((err) => {
            dispatch({ type: SET_TEMPLATE_LIST, data: [] });
            reject(err);
          });
      });
    },
    /* 增加模板 */
    addTemplateTaskList(params) {
      return new Promise((resolve, reject) => {
        addDetectionTemplate(params)
          .then((res) => {
            const { data } = res;
            dispatch({ type: ADD_TEMPLATE, data });
            resolve(data);
          })
          .catch((err) => reject(err));
      });
    },
    /* 修改模板 */
    editTemplateTask(params) {
      return new Promise((resolve, reject) => {
        editeDetectionTemplate(params)
          .then((res) => {
            const { data } = res;
            dispatch({ type: EDIT_TEMPLATE, data });
            resolve(res);
          })
          .catch((err) => reject(err));
      });
    },
    /**
     * @description: 删除
     * @param {*} id 删除的id
     * @param {*} content 弹窗展示内容
     * @return {*}
     */
    deleteTemplateFunc(id, content = '确认删除该检测模板？') {
      Modal.confirm({
        title: '删除',
        content,
        onOk() {
          const ids = id instanceof Array ? id.join(',') : id;
          deleteDetectionTemplate(ids).then((res) => {
            message.success('移除成功');
            const data = res.data.map((id) => parseInt(id, 10));
            dispatch({ type: DELETE_TEMPLATE, ids: data });
          });
        },
        onCancel() {
          console.log('Cancel');
        }
      });
    }
  };
  const filters = {
    typeFilter(id) {
      return state.typeStatusList.find((item) => item.code === id)?.name || '-';
    }
  };
  useEffect(() => {}, []);
  return (
    <DetectionTemplateContext.Provider value={{ ...state, ...actions, ...filters }}>
      {props.children}
    </DetectionTemplateContext.Provider>
  );
};
export const useDetectionTemplateContext = () => {
  return useContext(DetectionTemplateContext);
};
export { DetectionTemplateContext, DetectionTemplateContextProvider };
